<template>
	<view class="waterfall" :style="'background-color:'+bgColor">
		<!-- <view v-for="(item, index) in columnData" :key="index" class="column" :style="{ width: columnWidth + 'px' }"> -->
		<view v-for="(item, index) in columnData" :key="index" class="column list_item">
			<view v-for="(childItem, childIndex) in item" class="column" :key="childIndex" style="width: 100%;" :id="'item' + childItem.id" @click.stop="click(childItem)">
				<view class="water_con" :style="'background-color:'+cardBgColor+';border-radius:'+radius+'rpx;'">
					<!-- <image :src="childItem.thumb" mode="widthFix" :style="{height:'200rpx',width: '100%'}"> -->
					<image :src="childItem.thumb" mode="widthFix"></image>
					<text class="list_title clamp2">{{childItem.title}}</text>
					<view class="l_cart">
						<text class="money"><text class="small">¥</text>{{Number(childItem.agentprice)}}<text class="small" v-if="childItem.hasoption==1">起</text></text>
						<text class="price_old">￥{{Number(childItem.productprice)}}</text>
					</view>
					<view class="l_butie">
						<text>最高补贴{{Number(childItem.btrate)}}%</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			//数据源
			dataList: {
				type: Array,
				required: true,
				default: []
			},
			//显示列数
			column: {
				type: Number,
				required: true,
				default: 2
			},
			//卡片margin(rpx)
			margin: {
				type: Number,
				default: 10
			},
			//卡片圆角(rpx)
			radius: {
				type: Number,
				default: 8
			},
			//页面背景颜色
			bgColor: {
				type: String,
				default: '#f2f2f2'
			},
			//卡片背景颜色
			cardBgColor: {
				type: String,
				default: '#FFFFFF'
			},
		},
		data() {
			return {
				columnData: [],
				columnWidth: 0,
				loading: false,
			};
		},
		watch: {
			dataList: {
				immediate: true,
				deep: true,
				handler(newValue, oldValue) {
					this.$nextTick(()=>{
						this.setColumnWidth()
						this.setData()
					})
				},
			},
			column: {
				immediate: false,
				deep: true,
				handler(newValue) {
					this.$nextTick(()=>{
						this.setColumnWidth()
						this.setData()
					})
				},
			},
		},
		methods: {
			//计算每列的高度
			getElemHeight(index) {
				this.$nextTick(() => {
					var arr = [];
					this.dataList.map((item, index) => {
						// uni.getImageInfo({
						// 	src: item.thumb,
						// 	success: (e) => {
						// 		item.height = (e.height * (this.columnWidth / e.width)) + 'px'
						// 		this.createSelectorQuery().select('#item' + item.id)
						// 			.boundingClientRect(res => {
						// 				arr.push({
						// 					...{
						// 						itemHeight: res.height
						// 					},
						// 					...item
						// 				});
						// 				if (arr.length == this.dataList.length) {
						// 					this.columnData = this.distributeToNArrays(arr,
						// 						this.column);
						// 				}
						// 			}).exec();
						// 	}
						// })
					})
				})
			},
			distributeToNArrays(arr, n) {
				let sums = new Array(n).fill(0);
				return arr.reduce(
					(arrays, item) => {
						let minSum = Math.min(...sums);
						let minIndex = sums.indexOf(minSum);
						arrays[minIndex].push(item);
						sums[minIndex] += item.itemHeight;
						return arrays;
					},
					new Array(n).fill().map(() => []),
				)
			},
			setColumnWidth() {
				// let width = uni.getSystemInfoSync().windowWidth
				// this.columnWidth = Math.floor(width / this.column)
			},
			setData() {
				const resultArray = this.dataList.reduce(
					(acc, cur, index) => {
						const targetIndex = index % this.column;
						acc[targetIndex].push(cur);
						return acc;
					},
					Array.from(Array(this.column), () => []),
				);
				this.columnData = resultArray
				this.getElemHeight()
			},
			click(item) {
				this.$emit('click', item)
			}
		},
	};
</script>

<style scoped lang="scss">
	.list_item {
		border-radius: 20upx;
		box-sizing: border-box;
		// margin: 0 10upx 20upx;
		padding-bottom: 20upx;
		position: relative;
		width: calc(50% - 10rpx);
		.water_con{
			margin-bottom: 20rpx;
			padding-bottom: 20rpx;
			width: 100%;
		}
		image {
			border-radius: 10upx;
			width: 100%;
		}
		.list_title {
			// font-weight: bold;
			overflow: hidden;
			// line-height: 45rpx;
			padding: 0 10upx;
		}
		.money {
			padding: 10upx;
			color: #AF141D;
			font-size: 40rpx;
			// font-weight: bold;
			.small{
				font-size: 20rpx;
			}
		}
		.price_old{
			text-decoration: line-through;
			font-size: 22upx;
			color: #999;
		}
		.l_cart {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
		}
		.l_butie{
			padding:0 10rpx;
			text{
				color: #8405dc;
				font-size: 26rpx;
				font-weight: bold;
			}
		}
	}
	
	.waterfall {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 20rpx 0;
	}

	.column {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.item {
		overflow: hidden;
	}

	.title-info {
		padding: 0rpx 20rpx 20rpx 20rpx;
	}

	.item-title {
		/* font-size: 30rpx; */
		color: #333333;
		/* line-height: 46rpx; */
		/* text-align: justify; */
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
	}

	.item-desc {
		margin-top: 4rpx;
		font-size: 26rpx;
		color: #666666;
		line-height: 34rpx;
		text-align: justify;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}
</style>